import { defineConfig, loadEnv, type ConfigEnv } from "vite";
import { resolve } from "path";

import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

import viteMock from "vite-easy-mock";

// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv) => {
  // vite脚手架中通过loadEnv函数加载env
  // 项目代码是通过import.meta.env加载env
  const env = loadEnv(mode.mode, process.cwd());

  return {
    plugins: [
      vue(),
      vueJsx(),
      // 拦截/mock开头的请求，去本地mock文件夹中找对应的资源，找到响应，找不到404
      viteMock({
        dir: "",
        pattern: "/mock",
      }),

    ],
    define: { 'process.env': {} },
    resolve: {
      // 配置路径别名
      alias: {
        "@": resolve(__dirname, "src"),
      },
      // 配置文件扩展名
      extensions: [".ts", ".vue", ".js", ".jsx", ".tsx"], // 导入时想要省略的扩展名列表。
    },
    css: {
      preprocessorOptions: {
        scss:{
          //引入var.scss全局预定义变量
          additionalData:'@import "./src/vars.scss";'
        }
      },
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
    server: {
      host:"0.0.0.0",
      port: +env.VITE_PORT,
      open: env.VITE_OPEN === "true",
      // 配置代理服务器
      proxy: {
        "/api": {
          target: "http://192.168.3.86:8080",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/file": {
          target: "https://www1.hkexnews.hk",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/file/, ""),
        },
      },
    },
    // pages:{
    //   move: {
    //     template: "./move.html",
    //     entry: "src/views/move/move.main.js",
    //     filename: "move.html",
    //     title: "move",
    //     keywords: "333",
    //     description: "444",
    // },
    // index: {
    //     template: "./index.html",
    //     entry: "src/main.js",
    //     filename: "index.html",
    //     title: "index",
    //     keywords: "333",
    //     description: "444",
    // }
    // }
    build:{
      chunkSizeWarningLimit:1500,
      rollupOptions: {
        output:{
            manualChunks(id) {
              if (id.includes('node_modules')) {
                  return id.toString().split('node_modules/')[1].split('/')[0].toString();
              }
          }
        }
    }

    }
  };
});
